import {LocalStorage} from 'quasar';
import {defaultValue} from 'src/lib/util/team.js'
import {extend} from 'quasar';
import Vue from 'vue';

const save = (state) => {
  LocalStorage.set('team', state.teams);
}

export const add = (state, payload) => {
  const d = defaultValue();
  const team = {};
  for (const i in d) {
    team[i] = payload[i];
  };
  state.teams.push(extend(true, team, {}));
  save(state);
}

export const update = (state, payload) => {
  const d = defaultValue();
  const team = {};
  for (const i in d) {
    team[i] = payload.team[i];
  };
  Vue.prototype.$set(state.teams, payload.index, extend(true, team, {}));
  save(state);
}

export const updateAll = (state, payload) => {
  state.teams = [];
  for (const i of payload) add(state, extend(true, i, {}));
}

export const remove = (state, payload) => {
  for (const i in state.teams) {
    if (i == payload) {
      state.teams.splice(i, 1);
      save(state);
      return;
    }
  }
}

export const importTeams = (state, payload) => {
  for (const i of payload) add(state, i);
}
